﻿using System;
using FileUtility.FTP.FtpSupport;
using System.Collections.Specialized;
using FileMonitor.Configuration.Prototype;
using FileMonitor.Configuration.Prototype.BizProcess;
namespace MonitorProvider.Prototype
{
    internal class DespatchFTPProcess : IProcessFile, IDisposable
    {
        #region Private Members
        private FtpConnection ftpConnection;
        private string _ftpFolderPath;
        private bool isInitialize;
        #endregion

        #region Public Properties
        public string FtpFolderPath
        {
            get
            {
                return this._ftpFolderPath;
            }
            set
            {
                this._ftpFolderPath = value;
            }
        }
        #endregion

        #region Constuctor
        public DespatchFTPProcess()
        {
            
        }
        #endregion

        #region ProcessFileBase Members

        private void Initialize(PlantProcessConfiguration plantProcessConfig)
        {
            if ((!string.IsNullOrEmpty(plantProcessConfig.FTPPath)))
            { 
                string ftpPath = plantProcessConfig.FTPPath;
                string ftpUserName = plantProcessConfig.FTPUserName;
                string ftpPassword =plantProcessConfig.FTPPasswrod;
                int iPort = plantProcessConfig.FTPPort;

                if (!string.IsNullOrEmpty(plantProcessConfig.FTPFolderPath))
                {
                    this._ftpFolderPath = plantProcessConfig.FTPFolderPath;
                }

                ftpConnection = new FtpConnection(ftpPath, ftpUserName, ftpPassword, iPort);
                ftpConnection.SetCurrentDirectory(this._ftpFolderPath);
                this.isInitialize = true;
            }
            else
            {
                throw new Exception("FTP Conntection Error");
            }
           // base(processType, fileProcessConfig);
        }

        public ExcutedTask ProcessFile(PlantProcessConfiguration plantProcessConfig, FileProcessConfiguration fileProcessConfig, string PlantCode, string FileName, string filePath)
        {
            Initialize(plantProcessConfig);
            this.ftpConnection.PutFile(filePath, FileName);
            ExcutedTask task = new ExcutedTask();
            task.Owner = PlantCode;
            task.FileName = FileName;
            return task;
        }
         #endregion

        #region IDisposable Member
        public void Dispose()
        {
            this.ftpConnection = null;
        }
        #endregion
    }

}
